$(function(){

    //路有对应的执行方法
    var routerBack = {
        inbox:function(id){
            replaceRoute('inbox/tmp.html');
            id && (p.urlPara = id);
        },
        processed:function () {
            replaceRoute('processed/tmp.html');
        },
        resolution:function(){
            replaceRoute('resolution/tmp.html');
        },
        resolution_detail:function(id){
            replaceRoute('resolution/detail/tmp.html');
            id && (p.urlPara = id);
        },
        resolution_detail2:function(id){
            replaceRoute('resolution/detail2/tmp.html');
            id && (p.urlPara = id);
        },
        resolution_statistics_same:function(){
            replaceRoute('resolution/statistics/same/tmp.html');
        },
        resolution_statistics_reply:function(){
            replaceRoute('resolution/statistics/reply/tmp.html');
        },
        resolution_statistics_reply_detail:function(id){
            replaceRoute('resolution/statistics/reply/detail/tmp.html');
            id && (p.urlPara = id);
        },
        resolution_statistics_site:function(){
            replaceRoute('resolution/statistics/site/tmp.html');
        },
        resolution_inquiry:function(){
            replaceRoute('resolution/inquiry/tmp.html');
        },
        worldpay:function(){
            replaceRoute('worldpay/tmp.html');
        },
        worldpay_detail:function(id){
            replaceRoute('worldpay/detail/tmp.html');
            id && (p.urlPara = id);
        },
        worldpay_view:function(id){
            replaceRoute('worldpay/view/tmp.html');
            id && (p.urlPara = id);
        },
        coupon:function(){
            replaceRoute('coupon/tmp.html');
        },
        img:function(id){
            replaceRoute('img/tmp.html');
            id && (p.urlPara = id);
        },
        //切换路由时：清除其他页面插入到全局dom及状态！【后期处理其他问题】
        del:function(){
            if(!com.getEnv()){
                location.href = '/admin.php';
            }
            com.delDom(['.c-datepicker-picker','.layer-box']);
            jumpRole();
        },
    };

    var routes = {
        '/inbox': {
            '/':routerBack.inbox,
            '/:id':{
                on:function (id){
                    routerBack.inbox(id)
                }
            }
        },
        '/processed':routerBack.processed,
        '/resolution':{
            '/':routerBack.resolution,
            '/detail/:id':{
                on:function(id){
                    routerBack.resolution_detail(id)
                }
            },
            '/detail2':{
                '/:id':{
                    on:function(id){
                        routerBack.resolution_detail2(id)
                    }
                }
            },
            '/statistics':{
                '/':routerBack.resolution_statistics_same,
                '/same':routerBack.resolution_statistics_same,
                '/reply':{
                    '/':routerBack.resolution_statistics_reply,
                    '/detail/:id':{
                        on:function(id){
                            routerBack.resolution_statistics_reply_detail(id)
                        }
                    }
                },
                '/site':routerBack.resolution_statistics_site
            },
            '/inquiry':{
                '/':routerBack.resolution_inquiry
            }
        },
        '/worldpay':{
            '/':routerBack.worldpay,
            '/detail/:id':{
                on:function(id){
                    routerBack.worldpay_detail(id)
                }
            },
            '/view/:id':{
                on:function(id){
                    routerBack.worldpay_view(id)
                }
            }
        },
        '/coupon':routerBack.coupon,
        '/img':{
            '/':routerBack.img,
            '/:id':{
                on:function(id){
                    routerBack.img(id)
                }
            }
        }
    };

    var router = Router(routes);
    router.configure({
        on: getUserInfo,
        before:routerBack.del
    });
    router.init();




    //模板填充
    function replaceRoute(url){
        //var route = window.location.hash.slice(1);
        app.get(url,function(tmp,status){
            $(".main-content").html(tmp);
            //导航变更
            var name = url.split("/")[0];
            $("[data-name='"+name+"']").addClass('active').siblings(".content-list").removeClass('active');
        },function(status){
            console.log(status);
            alert('模板加载失败')
        });
    }
    //暂时获取用户信息
    function getUserInfo(){
        if($.isEmptyObject(para.user)){
            $.ajax({
                type:'get',
                url:app.api('/api/user/info/' + para.token),
                async:false,
                success:function(res){
                    if(res.code == 0){
                        para.user = res.data;
                        jumpRole();
                    }else{
                        console.log(res.msg)
                    }
                }
            });
        }
    }

    //权限跳转
    function jumpRole(){

        if(para.user && (para.user.role_id == 26 || para.user.role_id == 25)){
            $(".outsource").addClass('hide');
            var str = location.hash;
            var roleArr = ['#inbox','#processed'];
            var isTrue = false;
            $.each(roleArr,function(index,item){
               if(str.indexOf(item)>=0){
                   isTrue = true;
               }
            });
            if(!isTrue){
                location.href = '#processed';
            }
        }
    }

});